package com.itheima.web.servlet;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;
import com.itheima.web.base.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
/*                             _ooOoo_
 *                            o8888888o
 *                            88" . "88
 *                            (| -_- |)
 *                            O\  =  /O
 *                         ____/`---'\____
 *                       .'  \\|     |//  `.
 *                      /  \\|||  :  |||//  \
 *                     /  _||||| -:- |||||-  \
 *                     |   | \\\  -  /// |   |
 *                     | \_|  ''\---/''  |   |
 *                     \  .-\__  `-`  ___/-. /
 *                   ___`. .'  /--.--\  `. . __
 *                ."" '<  `.___\_<|>_/___.'  >'"".
 *               | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *               \  \ `-.   \_ __\ /__ _/   .-` /  /
 *          ======`-.____`-.___\_____/___.-`____.-'======
 *                             `=---='
 *          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *
 *                   佛祖保佑        永无BUG
 * *********************************************************************************************/
/**
    用户管理  CURD
 */
@WebServlet("/userServlet")
public class UserServlet extends BaseServlet {
   /* public void checkName(HttpServletRequest request, HttpServletResponse response) {
        //1.获取姓名
        String name = request.getParameter("name");
        //2.调用service查询
        UserService service = new UserServiceImpl();
        service.checkName(name);

    }*/

    /**
     * 删除选中
     * @param request
     * @param response
     */
    public void deleteSel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.获取选中用户的id
        String[] ids = request.getParameterValues("uid");
        //2.调用service删除
        UserService service =new UserServiceImpl();
        service.deleteSel(ids);

        //3. 删除之后, 再次查询
        response.sendRedirect(request.getContextPath() + "/userServlet?method=findAll");

    }

        public void update(HttpServletRequest request, HttpServletResponse response) {
        try {

            //2.获取所有请求参数
            Map<String, String[]> map = request.getParameterMap();
            //3.封装数据到对象中
            User user = new User();
            BeanUtils.populate(user, map);
            //4.调用service更新
            UserService service = new UserServiceImpl();
            service.update(user);
            //5.再次查询
            response.sendRedirect(request.getContextPath() + "/userServlet?method=findAll");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取查询用户ID
        String id = request.getParameter("id");

        //2.调用service查询
        UserService service = new UserServiceImpl();
        User user = service.findById(id);

        //3.将数据存入request域中
        request.setAttribute("user", user);

        //System.out.println(user);

        //4.请求转发到update.jsp页面,回显用户信息
        request.getRequestDispatcher("/update.jsp").forward(request, response);
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1. 获取删除用户ID
        String id = request.getParameter("id");
        //2. 调用service删除
        UserService service = new UserServiceImpl();
        service.deleteById(id);
        //3. 重定向再次查询
        response.sendRedirect(request.getContextPath() + "/userServlet?method=findAll");
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //1. 设置请求编码
            //request.setCharacterEncoding("UTF-8");
            //2. 获取所有请求参数Map集合
            Map<String, String[]> map = request.getParameterMap();
            //3. 封装数据到User对象
            User user = new User();
            BeanUtils.populate(user, map);
            //4. 调用service保存数据
            UserService service = new UserServiceImpl();
            service.save(user);

            //5. 重定向到 userListServlet , 再次查询. 重定向以后, 浏览器路径发生了改变, 可以防止表单重复提交
            response.sendRedirect(request.getContextPath() + "/userServlet?method=findAll");

            //request.getRequestDispatcher("/userListServlet").forward(request,response);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //0. 获取分页条件  null a  -1
        int pageSize = 5;
        try {
            pageSize = Integer.parseInt(request.getParameter("pageSize"));// 每页显示条数
            if(pageSize<1){
                pageSize = 5;
            }
        } catch (NumberFormatException e) {
            //e.printStackTrace();
        }


        int pageNum = 1;
        try {
            pageNum = Integer.parseInt(request.getParameter("pageNum"));// 每页显示条数
        } catch (NumberFormatException e) {
            //e.printStackTrace();
        }

        //1. 调用service查询
        UserService service = new UserServiceImpl();
        PageInfo<User> info = service.findAll(pageNum,pageSize);

        //2. 将数据保存到request域中
        request.setAttribute("pb", info);

        //3. 请求转发到list.jsp
        request.getRequestDispatcher("/list.jsp").forward(request, response);
    }

}
